//
//  PersonalFCViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/10.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "PersonalFCViewController.h"
#import "OtherPersonalViewController.h"

#import "HeadButton.h"
#import "ConcernView.h"
#import "FansView.h"
#import "Alert.h"
#import "NotView.h"

#import "AVRequest+FC.h"

@interface PersonalFCViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) HeadButton *headButton;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) ConcernView *concernView;
@property (nonatomic, strong) FansView *fansView;

@property (nonatomic, strong) NotView *notView;// 错误
@property (nonatomic, strong) NotView *concernNotView;// 没有关注
@property (nonatomic, strong) NotView *fansNotView;// 没有粉丝

@end

@implementation PersonalFCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    [self urlFansRequest];
    [self urlConcernRequest];
}

- (void)initializeAppearance {
    self.navigationBar.title.text = @"关注与粉丝";
    [self.view addSubview:self.headButton];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.concernView];
    [self.scrollView addSubview:self.fansView];
}

#pragma mark - URLRequest

- (void)urlFansRequest {
    [AVRequest avFansWithObj:self.obj objArr:^(NSArray *objects, NSError *error) {
        if (error) {
            [[RequestError sharedError] showErrorWithTitle:@"请求出错"];
            [self.view addSubview:self.notView];
            return;
        }
        if (objects.count == 0) {
            [self.scrollView addSubview:self.fansNotView];
            return;
        }
        
        [self.notView removeFromSuperview];
        [self.fansView.dataArr addObjectsFromArray:objects];
        [self.fansView.tableView reloadData];
    }];
}

- (void)urlConcernRequest {
    [AVRequest avConcernWithObj:self.obj objArr:^(NSArray *objects, NSError *error) {
        if (error) {
            [[RequestError sharedError] showErrorWithTitle:@"请求出错"];
            [self.view addSubview:self.notView];
            return;
        }
        if (objects.count == 0) {
            [self.scrollView addSubview:self.concernNotView];
            return;
        }
        
        [self.notView removeFromSuperview];
        [self.concernView.dataArr addObjectsFromArray:objects];
        [self.concernView.tableView reloadData];
    }];
}

- (void)urlRemoveConcernWithObj:(AVObject *)obj {
    NSInteger count = [[MyUser sharedUser].concernCount integerValue] - 1;
    NSString *countStr = [NSString stringWithFormat:@"%ld", count];
    [self.obj setObject:countStr forKey:@"concernCount"];
    [AVRequest avRemoveConcernWithObj:self.obj removeObj:obj saveSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"取消失败" message:nil];
            return;
        }
        
        if (succeed) {
            [self.concernView.dataArr removeObject:obj];
            [self.concernView.tableView reloadData];
            [MyUser sharedUser].concernCount = [NSString stringWithFormat:@"%ld", [[MyUser sharedUser].concernCount integerValue] - 1];
        }else {
            [Alert alertWithTitle:@"取消失败" message:nil];
        }
    }];
}

#pragma mark - respondsTo..
// 100 = 关注   101 = 粉丝
- (void)respondsToHead:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    
    sender.selected = YES;
    if (sender.tag == 100) {
        self.headButton.right.selected = NO;
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else {
        self.headButton.left.selected = NO;
        [self.scrollView setContentOffset:CGPointMake(kWIDTH, 0) animated:YES];
    }
}
// 点击cell YES = concern
- (void)respondsToCell:(NSIndexPath *)indexPath is:(BOOL)is {
    AVObject *obj;
    if (is) {
        obj = self.concernView.dataArr[indexPath.row];
    }else {
        obj = self.fansView.dataArr[indexPath.row];
    }
    OtherPersonalViewController *personal = [[OtherPersonalViewController alloc] init];
    personal.obj = obj;
    [self.navigationController pushViewController:personal animated:YES];
}

// 取消关注
- (void)respondsToCancel:(NSIndexPath *)indexPath {
    AVObject *obj = self.concernView.dataArr[indexPath.row];
    [self urlRemoveConcernWithObj:obj];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x/2;
    CGRect rect = self.headButton.line.frame;
    rect.origin.x = x;
    self.headButton.line.frame = rect;
    
    if (x * 2 == 0) {
        self.headButton.left.selected = YES;
        self.headButton.right.selected = NO;
    }else if (x * 2 == kWIDTH) {
        self.headButton.left.selected = NO;
        self.headButton.right.selected = YES;
    }
}

#pragma mark - get

- (HeadButton *)headButton {
    
    if (!_headButton) {
        
        _headButton = [[HeadButton alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, 44)];
        _headButton.backgroundColor = [UIColor whiteColor];
        
        if (self.isFC) {
            _headButton.left.selected = YES;
        }else {
            _headButton.right.selected = YES;
            _headButton.line.frame = CGRectOffset(_headButton.line.bounds, kWIDTH/2 + 0.5, _headButton.bounds.size.height - 2);
        }
        
        __weak PersonalFCViewController *fc = self;
        _headButton.tapButton = ^(UIButton *sender) {
            [fc respondsToHead:sender];
        };
        
    }
    return _headButton;
}

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        CGFloat spacing = 64 + 44;
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, spacing, kWIDTH, kHEIGHT - spacing)];
        // 分页
        _scrollView.pagingEnabled = YES;
        // 滚动线
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        // 滚动内容范围
        _scrollView.contentSize = CGSizeMake(kWIDTH * 2, 0);
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        
        if (self.isFC) {
            [_scrollView setContentOffset:CGPointMake(0, 0)];
        }else {
            [_scrollView setContentOffset:CGPointMake(kWIDTH, 0)];
        }
        
    }
    return _scrollView;
}

- (ConcernView *)concernView {
    
    if (!_concernView) {
        
        _concernView = [[ConcernView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, CGRectGetHeight(self.scrollView.bounds))];
        _concernView.is = self.isSelf;
        
        __weak PersonalFCViewController *fc = self;
        _concernView.tapCell = ^(NSIndexPath *indexPath) {
            [fc respondsToCell:indexPath is:YES];
        };
        _concernView.tapCancel = ^(NSIndexPath *indexPath) {
            [fc respondsToCancel:indexPath];
        };
        
    }
    return _concernView;
}

- (FansView *)fansView {
    
    if (!_fansView) {
        
        _fansView = [[FansView alloc] initWithFrame:CGRectMake(kWIDTH, 0, kWIDTH, CGRectGetHeight(self.concernView.bounds))];
        
        __weak PersonalFCViewController *fc = self;
        _fansView.tapCell = ^(NSIndexPath *indexPath) {
            [fc respondsToCell:indexPath is:NO];
        };
        
    }
    return _fansView;
}

- (NotView *)notView {
    
    if (!_notView) {
        
        CGFloat y = 64 + CGRectGetHeight(self.headButton.bounds);
        _notView = [[NotView alloc] initWithFrame:CGRectMake(0, y, kWIDTH, kHEIGHT - y)];
        _notView.backgroundColor = [UIColor whiteColor];
        _notView.title.text = @"请求错误";
        
        __weak PersonalFCViewController *fc = self;
        _notView.tapRefresh = ^() {
            [fc urlConcernRequest];
            [fc urlFansRequest];
        };
        
    }
    return _notView;
}

- (NotView *)concernNotView {
    
    if (!_concernNotView) {
        
        CGFloat y = 64 + CGRectGetHeight(self.headButton.bounds);
        _concernNotView = [[NotView alloc] initWithFrame:CGRectMake(0, y, kWIDTH, (kHEIGHT - y)/3)];
        _concernNotView.backgroundColor = [UIColor whiteColor];
        [_concernNotView.refresh removeFromSuperview];
        _concernNotView.title.text = @"您还没有任何关注";
        
    }
    return _concernNotView;
}

- (NotView *)fansNotView {
    
    if (!_fansNotView) {
        
        CGFloat y = 64 + CGRectGetHeight(self.headButton.bounds);
        _fansNotView = [[NotView alloc] initWithFrame:CGRectMake(kWIDTH, y, kWIDTH, (kHEIGHT - y)/3)];
        [_fansNotView.refresh removeFromSuperview];
        _fansNotView.backgroundColor = [UIColor whiteColor];
        _fansNotView.title.text = @"您还没有任何粉丝";
        
    }
    return _fansNotView;
}

@end
















